package org.example.lanhang.controller;

import org.example.lanhang.VO.workervo;
import org.example.lanhang.entity.Worker;
import org.example.lanhang.service.IWorkerService;
import org.example.lanhang.util.PageResult;
import org.example.lanhang.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gww
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    private IWorkerService w;

    @RequestMapping("/list")
    public R getworker(){
        return R.ok().data("list",w.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize){
        PageResult<Worker> wo = w.findByPage(page,pageSize);
        return R.ok().data("wo",wo);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,@PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) workervo vo){
        PageResult<Worker>wo=w.search(page,pageSize,vo);
        return R.ok().data("wo",wo);
    }

    @PostMapping("/add")
    public R add(@RequestBody Worker worker){
        boolean flag=w.add(worker);
        return flag ? R.ok().message("添加员工信息成功") : R.error().message("添加员工信息失败");
    }

    @PutMapping("/update")
    public R update(@RequestBody Worker worker){
        boolean flag=w.updateById(worker);
        return flag ? R.ok().message("修改员工信息成功") :R.error().message("修改员工信息失败");
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return w.removeByIds(sids) ? R.ok().message("删除学生成功!") : R.error().message("删除学生失败!");
    }

}
